# 通用 Rust 服务 Dockerfile - 多阶段构建

# ========== 构建阶段 ==========
FROM rust:1.75-slim as builder

WORKDIR /workspace

# 安装构建依赖
RUN apt-get update && apt-get install -y \
    pkg-config \
    libssl-dev \
    && rm -rf /var/lib/apt/lists/*

# 复制依赖文件
COPY Cargo.toml Cargo.lock ./
COPY shared ./shared
COPY services ./services
COPY tests ./tests

# 构建服务（通过 --bin 参数指定）
ARG SERVICE_NAME
RUN cargo build --release --bin ${SERVICE_NAME}

# ========== 运行阶段 ==========
FROM debian:bookworm-slim

# 安装运行时依赖
RUN apt-get update && apt-get install -y \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# 从构建阶段复制二进制文件
ARG SERVICE_NAME
COPY --from=builder /workspace/target/release/${SERVICE_NAME} /app/service

# 设置环境变量
ENV RUST_LOG=info

# 暴露端口（通过运行时参数指定）
EXPOSE 8080

# 运行服务
CMD ["/app/service"]

